package cn.zhentao.controller;

import cn.zhentao.pojo.Farm;
import cn.zhentao.pojo.TUser;
import cn.zhentao.service.FarmService;
import cn.zhentao.service.TUserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基地控制层
 */
@RestController
@RequestMapping("farm")
public class FarmContro {
    @Autowired
    private FarmService farmService;
    @Autowired
    private TUserService tUserService;
    //根据id查询基地
    @GetMapping("/listbyuserid/{userId}")
    public List<Farm> listbyuserid(@PathVariable("userId") Integer userId){
        List<Farm> list = farmService.list(Wrappers.<Farm>lambdaQuery().eq(Farm::getUserid, userId));
        for (Farm farm : list) {
            TUser byId = tUserService.getById(farm.getUserid());
            farm.setUser(byId);
        }
        return list;
    }
    //查询所有
    @GetMapping("list")
    public List<Farm> list(){
        List<Farm> list = farmService.list();
        for (Farm farm : list) {
            TUser byId = tUserService.getById(farm.getUserid());
            farm.setUser(byId);
        }
        return list;
    }
    //添加基地
    @PostMapping("add")
    public String add(@RequestBody Farm farm){
       return farmService.save(farm)?"添加成功":"添加失败";
    }
    //修改基地
    @PostMapping("upda")
    public String upda(@RequestBody Farm farm){
        return farmService.updateById(farm)?"修改成功":"修改失败";
    }
    //删除基地
    @DeleteMapping("del/{fid}")
    public String del(@PathVariable("fid") Integer fid){
        return farmService.removeById(fid)?"删除成功":"删除失败";
    }
}
